<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no, shrink-to-fit=no">
  <title>DPlayer 直播播放</title>
  <style>
	  body,html{
		  padding: 0;
		  margin:0;
		  height: 100%;
	  }
	  iframe{
		 border:none !important;
	  }
  </style>
</head>
<body>
  <!-- 播放器容器 -->
  <div id="dplayer" style="height: 100%"></div>
  <script src="./Dplayer/dplayer/dist/DPlayer.min.js"></script>
  <script src="./flv.js/dist/flv.min.js"></script>
  <script>
    // 从 URL 参数获取直播地址
    function getUrlParam(name) {
      const params = new URLSearchParams(window.location.search);
	  console.log('params',params)
      return params.get(name);
    }
    if(getUrlParam('url')){
		console.log('url', getUrlParam('url') )
		//#ifdef H5
		// 检测MSE支持（FLV播放必需）
		if (!window.MediaSource) {
		  console.log('当前浏览器不支持MediaSource Extensions，无法播放FLV格式');
		}
		const dp = new DPlayer({
		    container: document.getElementById('dplayer'),
			live: true,
		    video: {
		        url: getUrlParam('url'),
				pic: getUrlParam('thumbnail'),
				autoplay: true,
				type: 'flv',
				flvjs: true, // 启用flv.js解码
			    customType: {
				  'flv': function (video, player) {
					if (flvjs.isSupported()) {
					  var flvPlayer = flvjs.createPlayer({
						type: 'flv',
						url: getUrlParam('url')
					  }, {
						enableStashBuffer: false,
						stashInitialSize: 128,
						isLive: true, // 如果是直播流，则设置为 true
						lazyLoad: true, // 延迟加载，减少内存占用
						deferLoadAfterSourceOpen: true // 延迟加载直到开始播放
					  });
					  flvPlayer.attachMediaElement(video);
					  flvPlayer.load();
					  video.pause(); // 初始暂停视频，等待 flv.js 加载完成后再播放
					  return flvPlayer;
					} else {
					  alert('Your browser is not supported!');
					}
				  }
				}
		    },
			onError: function(event) {  
			    console.error('播放错误', event);  
			}  
		});
		dp.on('waiting', function () {
		    console.log('player ended');
		});
		dp.on('error', function () {
		    console.log('player error');
		});
		//#endif
	}

  </script>
</body>
</html>